/*
 * This source code is part of TWaver 4.0
 *
 * Serva Software PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * Copyright 2002 - 2011 Serva Software. All rights reserved.
 */

package demo.network.chassis.dynamic;

import java.awt.event.MouseEvent;
import java.io.IOException;

import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;

import twaver.AlarmSeverity;
import twaver.Card;
import twaver.Element;
import twaver.PopupMenuGenerator;
import twaver.Port;
import twaver.TDataBox;
import twaver.TView;
import twaver.TWaverUtil;
import twaver.TaskAdapter;
import twaver.TaskScheduler;
import twaver.Node;
import twaver.network.TNetwork;
import twaver.tree.TTree;
import demo.DemoPane;


public class DynamicChassisDemo extends DemoPane {
	private TDataBox box = new TDataBox();
	private TNetwork network = new TNetwork(box);
    private TTree tree = new TTree(box);
    private JSplitPane split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
                                              new JScrollPane(tree),
                                              network);
    public DynamicChassisDemo() {
        this.add(split);
        this.tree.setEnsureVisibleOnSelected(true);

        //load cisco chassis from xml file.
        try {
			box.parse("/demo/network/chassis/dynamic/cisco_12000.xml");
		} catch (IOException e) {
			e.printStackTrace();
			return;
		}

        //simulate alarms on port 'fiber1' and 'fiber2'.
        TaskScheduler.getInstance().register(new TaskAdapter() {
            public int getInterval() {
                return 1000;
            }

            public void run(long clock) {
            	if(!network.getCanvas().isShowing()){
            		return;
            	}
                box.getElementByTag("fiber1").getAlarmState().addNewAlarm(AlarmSeverity.CRITICAL);
                box.getElementByTag("fiber2").getAlarmState().addNewAlarm(AlarmSeverity.MAJOR);
            }
        });

        //simulate the card change on card 'card1'.
        TaskScheduler.getInstance().register(new TaskAdapter() {
            private Element card = box.getElementByTag("card1");
            private boolean remove = true;

            public int getInterval() {
                return 1000;
            }

            public void run(long long0) {
                if (remove) {
                    box.removeElement(card);
                } else {
                	card.setParent(box.getElementByID("rackID"));
                    box.addElement(card);
                }
                remove = !remove;
            }
        });

        //simulate the port status change event, change its color.
        TaskScheduler.getInstance().register(new TaskAdapter() {
            public int getInterval() {
                return 500;
            }

            public void run(long long0) {
            	if(!network.getCanvas().isShowing()){
            		return;
            	}
            	((Node)box.getElementByTag("testport1")).putRenderColor(TWaverUtil.getRandomAlphaColor());
                ((Node)box.getElementByTag("outlineTestPort")).putStateOutlineColor(TWaverUtil.getRandomAlphaColor());
                ((Node)box.getElementByTag("outlineTestPort")).putDrawIconShape(TWaverUtil.getRandomBool());
                ((Node)box.getElementByTag("outlineTestPort")).putDrawImageShape(TWaverUtil.getRandomBool());
            }
        });

        //simulate event on port 'testport2', show it as attachment icon.
        TaskScheduler.getInstance().register(new TaskAdapter() {
            Element port = box.getElementByTag("testport2");

            public int getInterval() {
                return 1000;
            }

            public void run(long long0) {
            	String attachmentName = "attachment" + (TWaverUtil.getRandomInt(7) + 1);
            	if(TWaverUtil.getRandomBool()){
            		port.addAttachment(attachmentName);
            	}else{
            		port.removeAttachment(attachmentName);
            	}
            }
        });

        //setup the popup menu factory.
        network.setPopupMenuGenerator(new PopupMenuGenerator() {
        	public JPopupMenu generate(TView tview, MouseEvent mouseEvent) {
                //for the demo, only one element selected we popup the menu.
                if (tview.getDataBox().getSelectionModel().size() != 1) {
                    return null;
                }
                //only the selected element is card or port instance, popup menu.
                Element element = tview.getDataBox().getLastSelectedElement();
                if (!(element instanceof Card) && !(element instanceof Port)) {
                    return null;
                }

                JPopupMenu menu = new JPopupMenu();
                if (element instanceof Card) {
                    menu.add(new JMenuItem("Card Popup Menu"));
                    //add more menu item here.
                }
                if (element instanceof Port) {
                    menu.add(new JMenuItem("Port Flow"));
                    menu.add(new JMenuItem("Turn Off/On"));
                    menu.addSeparator();
                    menu.add(new JMenuItem("Alarm..."));
                }

                return menu;
            }
        });
    }

}